package com.tom.demo.service.system.impl;

import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tom.demo.common.entity.SysPost;
import com.tom.demo.common.enumeration.ResponseEnum;
import com.tom.demo.common.exception.CustomException;
import com.tom.demo.common.response.Result;
import com.tom.demo.common.utils.SecurityUtils;
import com.tom.demo.mapper.SysPostMapper;
import com.tom.demo.service.system.PostService;
import com.tom.demo.vo.SysPostVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PostServiceImpl implements PostService {
    @Resource
    private SysPostMapper sysPostMapper;

    @Resource
    private Snowflake snowflake;

    @Override
    public Result getPostPageList(Integer page, Integer pageSize, SysPost sysPost) {
        PageHelper.startPage(page,pageSize);
        List<SysPostVo> sysPostVos = sysPostMapper.pageList(sysPost);
        PageInfo<SysPostVo> pageInfo = new PageInfo<>(sysPostVos);
        return Result.success(pageInfo);
    }

    @Override
    public Result insertPost(SysPost sysPost) {
        String postCode = sysPost.getPostCode();
        int codeOne = sysPostMapper.oneCount(null,null , postCode);
        if(codeOne > 0){
            throw new CustomException(ResponseEnum.POST_CODE_DATA_EXIST);
        }
        String postName = sysPost.getPostName();
        int nameOne = sysPostMapper.oneCount(null, postName,null);
        if(nameOne > 0){
            throw new CustomException(ResponseEnum.POST_NAME_DATA_EXIST);
        }
        sysPost.setPostId(snowflake.nextIdStr());
        sysPost.setCreateTime(new Date());
        sysPost.setCreateBy(SecurityUtils.getLoginUserName());
        int row = sysPostMapper.insert(sysPost);
        return row > 0 ? Result.success():Result.error("添加岗位失败!");
    }

    @Override
    public Result updatePost(SysPost sysPost) {
        String postId = sysPost.getPostId();
        if(!StringUtils.hasText(postId)){
            return Result.error("postId不能为空");
        }
        SysPost post = sysPostMapper.selectByPrimaryKey(postId);
        sysPost.setCreateBy(post.getCreateBy());
        sysPost.setCreateTime(post.getCreateTime());
        String postCode = sysPost.getPostCode();
        int codeOne = sysPostMapper.oneCount(postId, null, postCode);
        if(codeOne>0){
            throw new CustomException(ResponseEnum.POST_CODE_DATA_EXIST);
        }
        String postName = sysPost.getPostName();
        int nameOne = sysPostMapper.oneCount(postId, postName,null);
        if(nameOne > 0){
            throw new CustomException(ResponseEnum.POST_NAME_DATA_EXIST);
        }
        sysPost.setUpdateTime(new Date());
        sysPost.setUpdateBy(SecurityUtils.getLoginUserName());
        int row = sysPostMapper.updateByPrimaryKey(sysPost);
        return row > 0 ? Result.success():Result.error("修改岗位失败!");
    }

    @Override
    public Result deleteByPostId(String postId) {
        int row = sysPostMapper.deleteByPrimaryKey(postId);
        return row > 0 ? Result.success():Result.error("删除岗位失败!");
    }
}
